package com.ruoyi.project.game.category.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.game.category.domain.Category;
import com.ruoyi.project.game.category.mapper.CategoryMapper;
import com.ruoyi.project.game.category.service.ICategoryService;
import com.ruoyi.project.game.tag.domain.Tag;

/**
 * 游戏分类Service业务层处理
 * 
 * @author shixiangwen
 * @date 2020-02-11
 */
@Service
@DataSource(value = DataSourceType.GAME)
public class CategoryServiceImpl implements ICategoryService {
	@Autowired
	private CategoryMapper categoryMapper;

	/**
	 * 查询游戏分类
	 * 
	 * @param id 游戏分类ID
	 * @return 游戏分类
	 */
	@Override
	public Category selectCategoryById(Long id) {
		return categoryMapper.selectCategoryById(id);
	}

	/**
	 * 查询游戏分类列表
	 * 
	 * @param category 游戏分类
	 * @return 游戏分类
	 */
	@Override
	public List<Category> selectCategoryList(Category category) {
		return categoryMapper.selectCategoryList(category);
	}

	/**
	 * 新增游戏分类
	 * 
	 * @param category 游戏分类
	 * @return 结果
	 */
	@Override
	public int insertCategory(Category category) {
		category.setCreateTime(DateUtils.getNowDate());
		category.setCreateBy(ShiroUtils.getLoginName());
		return categoryMapper.insertCategory(category);
	}

	/**
	 * 修改游戏分类
	 * 
	 * @param category 游戏分类
	 * @return 结果
	 */
	@Override
	public int updateCategory(Category category) {
		category.setUpdateTime(DateUtils.getNowDate());
		category.setUpdateBy(ShiroUtils.getLoginName());
		return categoryMapper.updateCategory(category);
	}

	/**
	 * 删除游戏分类对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteCategoryByIds(String ids) {
		return categoryMapper.deleteCategoryByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除游戏分类信息
	 * 
	 * @param id 游戏分类ID
	 * @return 结果
	 */
	@Override
	public int deleteCategoryById(Long id) {
		return categoryMapper.deleteCategoryById(id);
	}

	@Override
	public List<Category> selectCategorysByGameId(Long id) {
		List<Category> categorys = categoryMapper.selectCategoryList(new Category());
		List<Category> gameCategorys = categoryMapper.selectCategorysByGameId(id);
		Set<Long> gameCategoryIds = new HashSet<>();
		for (Category category : gameCategorys) {
			gameCategoryIds.add(category.getId());
		}
		for (Category category : categorys) {
			if (gameCategoryIds.contains(category.getId())) {
				category.setFlag(true);
			}
		}
		return categorys;
	}

}
